/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MainPackage.Views;

import MainPackage.Options;
import MainPackage.User;
import MainPackage.UserGewicht;
import MainPackage.Userlist;
import java.awt.GridLayout;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;

/**
 *
 * @author n.kaminski d.chauvistre
 * wichtiger link: http://www.java2s.com/Code/Java/Chart/JFreeChartAreaChartDemo.htm
 *                 http://sourceforge.net/projects/pswing/files/
 */

public class GewichtView extends javax.swing.JDialog {

    private User user;
    private Userlist UList;
    /**
     * Creates new form GewichtView
     */
    public GewichtView(User user, Userlist pUList, java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.user = user;
        this.UList = pUList;
        initComponents();
        
        DrawChartPanel.setLayout(new GridLayout(1,1));
        this.reloadBmi();
        this.reloadChart();

    }
    
    private void reloadBmi(){
        if(user.getUsergewicht().Count()==0){
            this.gewicht.setText("k.A");
        }
        else{
            this.gewicht.setText(""+user.getUsergewicht().getGewicht()+" kg");
        }
        this.groesse.setText(""+user.getGroesse()/100.0+" m");
        this.BMI.setText(""+getBmi());
        this.kategorie.setText(bmiKategorie());
        this.alter.setText(""+user.getAlter());
    }
    private void reloadChart(){
        final TimeSeries series1 = new TimeSeries("Random 1");
        double value = 0.0;
        Day day = new Day();
        //Day d = new Day(new Date());
        /*for (int i = 0; i < 200; i++) {
            value = value + Math.random() - 0.5;
            series1.add(day, value);
            day = (Day) day.next();
        }*/
        UserGewicht ug = user.getUsergewicht();
        
        ug.FillJTable(jTable1);
        
        int start = ug.Count()-60;
        if(start < 0 ){start = 0;}
        for(int i = start; i <  ug.Count(); ++i){
            //System.out.println(ug.getDate(i));            
            series1.add(new Day(ug.getDate(i)), ug.getGewicht(i));
        }
        
        final TimeSeriesCollection dataset = new TimeSeriesCollection(series1);

        final JFreeChart chart = createChart(dataset);

        //final ChartPanel chartPanel = new ChartPanel(chart);
        
        final ChartPanel chartPanel = new ChartPanel(chart, false, false, false, false, false);
        chartPanel.setPopupMenu(null);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        
        chartPanel.setEnabled(false);
        
        //System.out.println(DrawChartPanel.getComponentCount());
        DrawChartPanel.removeAll();
        DrawChartPanel.add(chartPanel);
    }
    
    /**
     * BMI Berechnung
     * @return BMI (gerundet auf 2 Nachkommastellen
     */
    private double getBmi(){
        if(user.getUsergewicht().Count()==0){
            return 0;
        }
        double b=user.getUsergewicht().getGewicht() /   //Gewicht in kg  /
                ((user.getGroesse()/100.0)*(user.getGroesse()/100.0));    // Größe in Metern   
        return Math.round(b*100)/100;
    }
    
    /**
     * Bedeutung des BMI mit Berücksichten des Alters
     * @return String Gewichtskategorie
     */
    private String bmiKategorie(){
        if(getBmi()==0){
            return "";
        }
        double bmi=getBmi();
        
        String art;
        
        // Berücksichtigen des Alters
        if(user.getAlter()>=25 && user.getAlter()<34){
            bmi-=1;
        }
        else if(user.getAlter()>=35 && user.getAlter()<44){
            bmi-=2;
        }
        else if(user.getAlter()>=45 && user.getAlter()<54){
            bmi-=3;
        }
        else if(user.getAlter()>=55 && user.getAlter()<64){
            bmi-=3;
        }
        else if(user.getAlter()>64){
            bmi-=4;
        }
        
        //BMI entsprechnende Kategorie
        if(bmi<=16){
            art="starkes Untergewicht";
        }
        else if(bmi>16 && bmi<=17){
            art="mäßiges Untergewicht";
        }
        else if(bmi>17 && bmi<=18.5){
            art="leichtes Untergewicht";
        }
        else if(bmi>18.5 && bmi<=25){
            art="Normalgewicht";
        }
        else if(bmi>25 && bmi<=30){
            art="Präadipositas (Übergewicht)";
        }
        else if(bmi>30 && bmi<=35){
            art="Adipositas Grad I";
        }
        else if(bmi>35 && bmi<40){
            art="Adipositas Grad II";
        }
        else if(bmi>=40){
            art="Adipositas Grad III";
        }
        else{
            art="keine Angabe";
        }
        
        return art;
    }
    

    
    /**
     * Creates a chart.
     * 
     * @param dataset  the dataset.
     * 
     * @return A chart.
     */
    private JFreeChart createChart(final XYDataset dataset) {
            
        final JFreeChart chart = ChartFactory.createXYAreaChart(
            "",
            "", "",
            dataset,
            PlotOrientation.VERTICAL,
            false,  // legend
            true,  // tool tips
            false  // URLs
        );
        
        final XYPlot plot = chart.getXYPlot();

        final ValueAxis domainAxis = new DateAxis("");
        domainAxis.setLowerMargin(0.0);
        domainAxis.setUpperMargin(0.0);
        plot.setDomainAxis(domainAxis);
        plot.setForegroundAlpha(0.5f); 
        
        
        final XYItemRenderer renderer = plot.getRenderer();
        renderer.setToolTipGenerator(
            new StandardXYToolTipGenerator(
                StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
                new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("#,##0.00")
            )
        );
        
        return chart;    
        
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFrame1 = new javax.swing.JFrame();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        EintrageDatum = new com.toedter.calendar.JDateChooser();
        jLabel6 = new javax.swing.JLabel();
        EintrageGewicht = new javax.swing.JTextField();
        SubmitGewichtEintrage = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        groesse = new javax.swing.JLabel();
        gewicht = new javax.swing.JLabel();
        BMI = new javax.swing.JLabel();
        kategorie = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        alter = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        DrawChartPanel = new javax.swing.JPanel();

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(180, 180, 180)));

        jLabel4.setBackground(new java.awt.Color(180, 180, 180));
        jLabel4.setText("Gewicht erfassen");
        jLabel4.setOpaque(true);

        jLabel5.setText("Datum");

        jLabel6.setText("Gewicht");

        SubmitGewichtEintrage.setText("Eintragen");
        SubmitGewichtEintrage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SubmitGewichtEintrageActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(SubmitGewichtEintrage)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(EintrageDatum, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(EintrageGewicht, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(EintrageDatum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(EintrageGewicht, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(SubmitGewichtEintrage)
                .addGap(0, 14, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(180, 180, 180)));

        jLabel7.setBackground(new java.awt.Color(180, 180, 180));
        jLabel7.setText("BodyMassIndex");
        jLabel7.setOpaque(true);

        jLabel1.setText("Größe:");

        jLabel2.setText("Gewicht:");

        jLabel3.setText("BMI:");

        jLabel8.setText("Kategorie:");

        groesse.setText("jLabel9");

        gewicht.setText("jLabel9");

        BMI.setText("jLabel9");

        kategorie.setText("jLabel9");

        jLabel9.setText("Alter:");

        alter.setText("jLabel10");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(groesse)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(gewicht)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(alter, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(kategorie)
                            .addComponent(BMI, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(groesse)
                    .addComponent(gewicht)
                    .addComponent(jLabel9)
                    .addComponent(alter))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(BMI))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(kategorie))
                .addGap(21, 21, 21))
        );

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        DrawChartPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        DrawChartPanel.setEnabled(false);

        javax.swing.GroupLayout DrawChartPanelLayout = new javax.swing.GroupLayout(DrawChartPanel);
        DrawChartPanel.setLayout(DrawChartPanelLayout);
        DrawChartPanelLayout.setHorizontalGroup(
            DrawChartPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        DrawChartPanelLayout.setVerticalGroup(
            DrawChartPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 231, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(DrawChartPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(DrawChartPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(195, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void SubmitGewichtEintrageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SubmitGewichtEintrageActionPerformed
        this.user.addGewicht(this.EintrageDatum.getDate(), Float.parseFloat(this.EintrageGewicht.getText()));
        UList.export();
        this.reloadChart();
        this.reloadBmi();
    }//GEN-LAST:event_SubmitGewichtEintrageActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel BMI;
    private javax.swing.JPanel DrawChartPanel;
    private com.toedter.calendar.JDateChooser EintrageDatum;
    private javax.swing.JTextField EintrageGewicht;
    private javax.swing.JButton SubmitGewichtEintrage;
    private javax.swing.JLabel alter;
    private javax.swing.JLabel gewicht;
    private javax.swing.JLabel groesse;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel kategorie;
    // End of variables declaration//GEN-END:variables
}
